/**
 * Copyright (C) 2010 Zhang, Guilin <guilin.zhang@hotmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.code.ministock.impl;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.ministock.Stock;

/**
 * <a href="http://hq.sinajs.cn/list=sh601006">Example URL</a>
 * @author <a href="mailto:guilin.zhang@hotmail.com">Zhang, Guilin</a>
 */
public class SinaStock implements Stock {
	
	private static final Logger logger = LoggerFactory.getLogger(SinaStock.class);
	
	private String name;
	
	private String[] informations;
	
	public SinaStock(String stock) {
		super();
		parse(stock);
	}

	private void parse(String stock) {
		name = StringUtils.substringBetween(stock, "str_", "=");
		logger.info("Retrieved {} information.", name);
		stock = StringUtils.substringBetween(stock, "\"");
		logger.info("Detailed information is \r\n{}", stock);
		informations = stock.split(",", 32);
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String getDisplayName() {
		return informations[0];
	}

	@Override
	public String getOpeningPriceOfToday() {
		return informations[1];
	}

	@Override
	public String getClosingPriceOfPreviousMarketDay() {
		return informations[2];
	}

	@Override
	public String getCurrentPrice() {
		return informations[3];
	}

	@Override
	public String getHighestPriceOfToday() {
		return informations[4];
	}

	@Override
	public String getLowestPriceOfToday() {
		return informations[5];
	}

	@Override
	public String getBiddingPrice(int sequence) {
		return informations[11 + (sequence % 5) * 2];
	}

	@Override
	public String getBiddingVolume(int sequence) {
		return informations[10 + (sequence % 5) * 2];
	}

	@Override
	public String getAuctionPrice(int sequence) {
		return informations[21 + (sequence % 5) * 2];
	}

	@Override
	public String getAuctionVolume(int sequence) {
		return informations[20 + (sequence % 5) * 2];
	}

	@Override
	public String getVolume(int unit) {
		Integer volume = Integer.valueOf(informations[8]) / unit;
		return volume.toString();
	}

	@Override
	public String getTurnOver(int unit) {
		Float turnOver = Float.valueOf(informations[9]) / unit;
		return turnOver.toString();
	}

	@Override
	public String getDate() {
		return informations[30];
	}

	@Override
	public String getTime() {
		return informations[31];
	}
}
